package cn.tedu.sims.controller;

import cn.tedu.sims.pojo.dto.InternalUnitDTO;
import cn.tedu.sims.pojo.dto.UnitPowerUpdateDTO;
import cn.tedu.sims.pojo.vo.InternalUnitListItemVO;
import cn.tedu.sims.pojo.vo.InternalUnitStandardVO;
import cn.tedu.sims.pojo.vo.LocationListItemVO;
import cn.tedu.sims.pojo.vo.UserListItemVO;
import cn.tedu.sims.service.InternalUnitService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "内部单位管理")
@Validated
@Slf4j
@RestController
@RequestMapping("/units")
public class InternalUnitController {
    @Autowired
    private InternalUnitService internalUnitService;

    @ApiOperation("添加内部单位")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/units/insert')")
    @PostMapping("/insert")
    public JsonResult<Void> insert(@Valid InternalUnitDTO internalUnitDTO){
        internalUnitService.insert(internalUnitDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据ID删除内部单位")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/units/id/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        internalUnitService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改内部单位权限")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/units/update/id')")
    @PostMapping("/update/rolePower")
    public JsonResult<Void> updateRolePower(UnitPowerUpdateDTO u){
        internalUnitService.updatePower(u);
        return JsonResult.ok();
    }

    @ApiOperation("查询内部单位")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/units')")
    @GetMapping("")
    public JsonResult<List<InternalUnitListItemVO>> list(){
        List<InternalUnitListItemVO> list = internalUnitService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据ID查询内部单位")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAuthority('/units/id')")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<InternalUnitStandardVO> ById(@PathVariable Long id){
        InternalUnitStandardVO unitStandardVO= internalUnitService.selectById(id);
        return JsonResult.ok(unitStandardVO);
    }

    @ApiOperation("模糊查询内部单位")
    @ApiOperationSupport(order = 402)
    @PreAuthorize("hasAuthority('/units/select')")
    @GetMapping("/select")
    public JsonResult<List<InternalUnitListItemVO>> selectByName(String name){
        List<InternalUnitListItemVO> list=internalUnitService.selectByName(name);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询内部单位所属用户")
    @ApiOperationSupport(order = 403)
     @PreAuthorize("hasAuthority('/units/select/user')")
    @GetMapping("/select/user")
    public JsonResult<List<UserListItemVO>> selectById( Long id){
        List<UserListItemVO> list=internalUnitService.selectByUser(id);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询地区")
    @ApiOperationSupport(order = 403)
     @PreAuthorize("hasAuthority('/units/select/region')")
    @GetMapping("/select/region")
    public JsonResult<List<LocationListItemVO>> selectByRegion(){
        List<LocationListItemVO> list=internalUnitService.selectByRegion();
        return JsonResult.ok(list);
    }

    @ApiOperation("查询已有权限")
    @ApiOperationSupport(order = 403)
     @PreAuthorize("hasAuthority('/units/power/exist/id')")
    @GetMapping("/power/exist/{id}")
    public JsonResult<List<Long>> exist(@PathVariable Long id){
        List<Long> list= internalUnitService.selectPowerExist(id);
        return JsonResult.ok(list);
    }



}
